package com.tanhua.dubbo.server.service;

import com.tanhua.dubbo.server.enums.IdType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

//⽣成⾃增⻓的id，原理：使⽤redis的⾃增⻓值
//圈子，小视频都可能自增长
@Service
public class IdService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 实现逻辑：借助于Redis中的自增长特性，实现全局的id自增长(唯一)
     * @return
     */
    //获取redis中key自增涨后的值
    public Long createId(IdType idType) {
        String idKey = "TANHUA_ID_" + idType.toString();
        return this.redisTemplate.opsForValue().increment(idKey);
    }
}
